//
//  SystemViewController.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/6/17.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class SystemViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!
    
    var modelList: [[SystemMessageModel]]? {
        didSet {
            self.setupView()
        }
    }
    
    var subViewList: [UIView] = []
    
    var offsetY: CGFloat = 0
    var page = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.scrollView!.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(SystemViewController.requestInfo))
        self.scrollView!.mj_footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(SystemViewController.nextRequestInfo))
        self.scrollView!.mj_header.beginRefreshing()
  
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.  
    }
    
    //MARK:networking
    func requestInfo(){
        self.page = 0
        DataTool.loadSystemMessageData { (modelArray) -> Void in
            self.scrollView.mj_header.endRefreshing()
            guard let newDataes = modelArray else {
                return
            }
            self.modelList = newDataes
        }

    }
    func nextRequestInfo(){
        self.page += 1
        let urlStr = String(format: "%@message/system/fenye/\(self.page)",BaseUrl)
        DataTool.loadNextSystemMessageData(String(self.page), urlStr: urlStr, completionHandler: { (modelArray) -> Void in
            self.scrollView.mj_footer.endRefreshing()
            guard let newDataes = modelArray else {
                return
            }
            self.modelList! += newDataes
        })

    }

    func setupView() -> Void {
        
        self.reset()
        
        let width = UIScreen.mainScreen().bounds.width
        
        for model in modelList! {
            if model.count == 1 {
                let view = SystemMessageViewOne.create(model[0])
                let viewWidth = width - 60
                let viewHeight = view.frame.size.height
                let x = (width / 2) - (viewWidth / 2)
                
                view.frame = CGRectMake(x, self.offsetY, viewWidth, viewHeight)
                self.scrollView.addSubview(view)
                
                self.offsetY += (viewHeight + 10)
                subViewList.append(view)
                
                view.delegate = self
            } else {
                let view = SystemMessageViewMultiple.create(model)
                let viewWidth = width - 60
                let viewHeight = view.frame.size.height
                let x = (width / 2) - (viewWidth / 2)
                
                view.frame = CGRectMake(x, self.offsetY, viewWidth, viewHeight)
                self.scrollView.addSubview(view)
                
                self.offsetY += (viewHeight + 10)
                subViewList.append(view)
                
                view.delegate = self
            }
        }
        
        self.scrollView.contentSize = CGSizeMake(UIScreen.mainScreen().bounds.width, offsetY + 100)
    }
    
    func reset() -> Void {
        self.offsetY = 0
        
        for view in subViewList {
            view.removeFromSuperview()
        }
    }
}

extension SystemViewController : SystemMessageDelegate {
    func onClick(id: Int) {
        let sb = UIStoryboard(name: "Message", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("SystemMessageDetailViewController") as! SystemMessageDetailViewController
        vc.id = String(id)
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
